package com.alienlab.finder.service.impl;

import com.alienlab.finder.domain.AppUser;
import com.alienlab.finder.domain.Relationship;
import com.alienlab.finder.repository.AppUserRepository;
import com.alienlab.finder.repository.RelationshipRepository;
import com.alienlab.finder.service.TemporaryPositionService;
import com.alienlab.finder.domain.TemporaryPosition;
import com.alienlab.finder.repository.TemporaryPositionRepository;
import com.alienlab.finder.service.dto.globalData;
import com.alienlab.finder.web.websocket.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * Service Implementation for managing TemporaryPosition.
 */
@Service
@Transactional
public class TemporaryPositionServiceImpl implements TemporaryPositionService {

    private final Logger log = LoggerFactory.getLogger(TemporaryPositionServiceImpl.class);

    private final TemporaryPositionRepository temporaryPositionRepository;
    private final AppUserRepository appUserRepository;
    private final RelationshipRepository relationshipRepository;

    public TemporaryPositionServiceImpl(RelationshipRepository relationshipRepository,AppUserRepository appUserRepository,TemporaryPositionRepository temporaryPositionRepository) {
        this.temporaryPositionRepository = temporaryPositionRepository;
        this.appUserRepository = appUserRepository;
        this.relationshipRepository = relationshipRepository;
    }

    /**
     * Save a temporaryPosition.
     *
     * @param temporaryPosition the entity to save
     * @return the persisted entity
     */
    @Override
    public TemporaryPosition save(TemporaryPosition temporaryPosition) {
        log.debug("Request to save TemporaryPosition : {}", temporaryPosition);
        return temporaryPositionRepository.save(temporaryPosition);
    }

    /**
     * Get all the temporaryPositions.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TemporaryPosition> findAll(Pageable pageable) {
        log.debug("Request to get all TemporaryPositions");
        return temporaryPositionRepository.findAll(pageable);
    }


    /**
     * Get one temporaryPosition by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<TemporaryPosition> findOne(Long id) {
        log.debug("Request to get TemporaryPosition : {}", id);
        return temporaryPositionRepository.findById(id);
    }

    /**
     * Delete the temporaryPosition by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TemporaryPosition : {}", id);
        temporaryPositionRepository.deleteById(id);
    }

    @Override
    public void lookingPosition(String admin_open_id,String guset_open_id,String flag,String type) throws Exception {
        AppUser guest_appUser = appUserRepository.findByOpenId(guset_open_id);
        if (guest_appUser==null){
            throw new Exception("未查找到opden_id为:"+guset_open_id+"的用户");
        }
        if (globalData.websocketmap.get(guest_appUser.getOpenId().toLowerCase())!=null){
            WebSocketServer.SendMessage(globalData.websocketmap.get(guest_appUser.getOpenId().toLowerCase()),admin_open_id+";"+flag+"查看"+type+"位置");
        }else {
            WebSocketServer.SendMessage(globalData.websocketmap.get(admin_open_id.toLowerCase()),"该用户不在线，无法查看当前位置信息");

        }

    }

    @Override
    public void sendPosition(String admin_open_id, String x, String y) throws Exception {


        AppUser main_appUser = appUserRepository.findByOpenId(admin_open_id);
        if (main_appUser==null){
            throw new Exception("未查找到opden_id为:"+admin_open_id+"的用户");
        }

        if (globalData.websocketmap.get(admin_open_id.toLowerCase())!=null){
            WebSocketServer.SendMessage(globalData.websocketmap.get(admin_open_id.toLowerCase()),"发送位置;"+x+","+y);
        }else {
            System.out.println("家长不在线，无法传递数据");

        }
    }
}
